<?php
class ObjektsController extends AppController{
	
	var $uses = array("Company","Objekt","City","Municipality");
	
	public function index(){
		
		$this->set('objekts',$this->Objekt->find('all'));
		
	}
	
	public function create(){
		
		if(!empty($this->params['named']['company'])){
			
			$conditions = array("Company.id" => $this->params['named']['company']);
			
		}elseif(!empty($this->data["Objekt"]["company_id"])){
			
			$conditions = array("Company.id" => $this->data["Objekt"]["company_id"]);			
			$objekt = $this->Objekt->save($this->data);
			
			if($objekt){
				$this->flash("Objektot e kreiran",array('controller'=>"companies",'action'=>"details",'company'=>$this->data['Objekt']['company_id']),2);
			}
		}
		
		if(!empty($conditions)){
			$params = array('conditions'=>$conditions);
			$company = $this->Company->find('first',$params);
			$this->set('company',$company);
			
			$select_options = array();
			
			$municipalities = $this->Municipality->find('all',array("conditions"=>array("Municipality.city_id" => $company['Company']['city_id'])));
			
			foreach($municipalities as $municipality){
				//var_dump($municipality['Municipality']);
				$select_options[$municipality['Municipality']['id']] = $municipality['Municipality']['name'];
			}
			
			//var_dump($select_options);
			
			$this->set('select_options',$select_options);
			
		}else{
			$this->redirect("/companies");
		}
	}
	
	public function edit(){
		
	}
	
	public function delete(){
		
	}
}